package com.Xht.www.dao;

import com.Xht.www.po.UserFavoritesPO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserFavoritesDAO extends BaseDAO {

    // 插入用户收藏信息
    public boolean insertUserFavorites(UserFavoritesPO userFavoritesPO) {
        if (!isValidUserFavorites(userFavoritesPO)) {
            return false;
        }
        String sql = "INSERT INTO user_favorites (user_id, merchant_name, favorite_reason, favorite_time) VALUES (?,?,?,?)";
        Connection connection = getConnection();
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, userFavoritesPO.getUserId());
            preparedStatement.setString(2, userFavoritesPO.getMerchantName());
            preparedStatement.setString(3, userFavoritesPO.getFavoriteReason());
            preparedStatement.setTimestamp(4, userFavoritesPO.getFavoriteTime());
            int rows = preparedStatement.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            releaseConnection(connection);
        }
    }

    // 删除用户收藏信息
    public boolean deleteUserFavorites(int favoriteId) {
        String sql = "DELETE FROM user_favorites WHERE favorite_id = ?";
        Connection connection = getConnection();
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, favoriteId);
            int rows = preparedStatement.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            releaseConnection(connection);
        }
    }

    // 检验输入是否合法有效
    private boolean isValidUserFavorites(UserFavoritesPO userFavoritesPO) {
        if (userFavoritesPO == null) {
            return false;
        }
        if (userFavoritesPO.getUserId() == null || userFavoritesPO.getUserId() <= 0) {
            return false;
        }
        if (userFavoritesPO.getMerchantName() == null || userFavoritesPO.getMerchantName().isEmpty()) {
            return false;
        }
        if (userFavoritesPO.getFavoriteReason() == null || userFavoritesPO.getFavoriteReason().isEmpty()) {
            return false;
        }
        return userFavoritesPO.getFavoriteTime() != null && !userFavoritesPO.getFavoriteTime().before(new Timestamp(0));
    }

    // 通过用户id获取收藏商家
    public List<UserFavoritesPO> getUserFavoritesByUserId(int userId) {
        List<UserFavoritesPO> favorites = new ArrayList<>();
        String sql = "SELECT * FROM user_favorites WHERE user_id = ? ORDER BY favorite_time DESC";
        Connection connection = getConnection();

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    UserFavoritesPO favorite = new UserFavoritesPO();
                    favorite.setFavoriteId(rs.getInt("favorite_id"));
                    favorite.setUserId(rs.getInt("user_id"));
                    favorite.setMerchantName(rs.getString("merchant_name"));
                    favorite.setFavoriteReason(rs.getString("favorite_reason"));
                    favorite.setFavoriteTime(rs.getTimestamp("favorite_time"));
                    favorites.add(favorite);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnection(connection);
        }

        return favorites;
    }
}